package edu.npu.cv.controller;

import edu.npu.cv.constant.TemplatePath;
import edu.npu.cv.dto.MemberDTO;
import edu.npu.cv.entity.MemberDO;
import edu.npu.cv.service.MemberService;
import edu.npu.cv.vo.DegreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lelechang
 * @description: 控制器层
 * @version: V1.0
 */
@RestController
@RequestMapping(TemplatePath.S + TemplatePath.PEOPLE)
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 从数据库获取成员列表
     */
    private List<MemberDO> getpeopleList() {
        return memberService.findAll();
    }

    /**
     * 查询所有成员
     * 
     * @return
     */
    @GetMapping
    public ModelAndView list(Model model) {
        List<MemberDO> peopleList = getpeopleList();
        List<DegreeVo> degreeVoList =new ArrayList<>();
        //将学位单独拿出来
        List<String> degreeList1 = peopleList.stream().map(MemberDO::getDegree).collect(Collectors.toList()).stream().collect(Collectors.toList());
        //去重
        List<String> degreeList = new ArrayList<>(new HashSet<String>(degreeList1));
//        model.addAttribute("Professor","教授");
//        model.addAttribute("Postdoctoral Fellow","博士后");
//        model.addAttribute("PhD Student","博士生");
//        model.addAttribute("Postgraduate Student","硕士研究生");
//        model.addAttribute("Undergraduate","本科生");
//        model.addAttribute("Alumni","已毕业学生");
        model.addAttribute("degreeList",degreeList);
        model.addAttribute("peopleList", peopleList );
        return new ModelAndView("people", "peopleModel", model);
    }

    /**
     * 根据ID查询成员
     * 
     * @return
     */
    @GetMapping("{id}")
    public ModelAndView view(@PathVariable("id") Long id, Model model) {
        MemberDO member = memberService.findById(id.longValue());
        model.addAttribute("people", MemberDTO.memberDO2DTO(member));
        return new ModelAndView("peopleview", "memberModel", model);
    }

}
